from table_gui import table_gui

class entry_fake :
    pass

class label_fake :
    def __init__ ( self ) :
        self . _background_history = [ ]
        self . _font_history = [ ]

class table_frame_fake :
    pass

class event_fake :
    _events_created = 0
    _events_deleted = 0
    def __init__ ( self , widget , handler ) :
        self . _widget = widget
        self . _handler = handler
        event_fake . _events_created += 1
    def __del__ ( self ) :
        event_fake . _events_deleted += 1

class mediator_fake :
    def __init__ ( self ) :
        self . _table_gui = None
        self . _labels = [ ]
        self . _table_rows_clicked = [ ]
        self . _relief_sunken = None
        self . _sticky_nesw = None
        self . _light_blue = None
        self . _events = [ ]
        self . _add_table_entry_reply = None
    def add_table_entry_reply ( self , entry ) :
        self . _add_table_entry_reply = entry
    def on_table_row_click ( self , row ) :
        self . _table_rows_clicked += [ row ]
    def gui_create_label_request ( self , master , text , borderwidth , relief , anchor ) :
        new_label = label_fake ( )
        new_label . _master = master
        new_label . _text = text
        new_label . _borderwidth = borderwidth
        new_label . _relief = relief
        new_label . _anchor = anchor
        self . _labels += [ new_label ]
        self . _table_gui . gui_create_label_reply ( new_label )
    def gui_create_entry_request ( self , master , borderwidth ) :
        new_entry = entry_fake ( )
        new_entry . _master = master
        self . _table_gui . gui_create_entry_reply ( new_entry )
    def gui_grid ( self , widget , column , row , sticky ) :
        widget . _in_grid = True
        widget . _sticky = sticky
        widget . _column = column
        widget . _row = row
    def gui_relief_sunken ( self ) :
        return self . _relief_sunken
    def gui_sticky_nesw ( self ) :
        return self . _sticky_nesw
    def gui_color_light_blue ( self ) :
        return self . _light_blue
    def gui_mouse_button_1_event_request ( self , widget , handler ) :
        new_event = event_fake ( widget , handler )
        self . _events += [ new_event ]
        self . _table_gui . gui_mouse_button_1_event_reply ( new_event )
    def gui_set_background_color ( self , widget , color ) :
        widget . _background_history += [ [ mediator_fake . gui_set_background_color , color ] ]
    def gui_push_background_color ( self , widget ) :
        widget . _background_history += [ [ mediator_fake . gui_push_background_color ] ]
    def gui_pop_background_color ( self , widget ) :
        widget . _background_history += [ [ mediator_fake . gui_pop_background_color ] ]
    def gui_set_widget_font_weight_to_bold ( self , widget ) :
        widget . _font_history += [ mediator_fake . gui_set_widget_font_weight_to_bold ]
    def gui_push_widget_font ( self , widget ) :
        widget . _font_history += [ mediator_fake . gui_push_widget_font ]
    def gui_pop_widget_font ( self , widget ) :
        widget . _font_history += [ mediator_fake . gui_pop_widget_font ]

class table_gui_tests :
    'table_gui'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _table_frame = table_frame_fake ( )
        self . _table_gui = table_gui ( self . _mediator )
        self . _mediator . _table_gui = self . _table_gui
        self . _table_gui . create_new_table_in_frame ( self . _table_frame )
        event_fake . _events_created = 0
        event_fake . _events_deleted = 0
    #
    # add_table_label
    #
    def test_add_table_label_master_frame ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _master , self . _table_frame )
    def test_add_table_label_text ( self ) :
        self . _table_gui . add_table_label ( 'my text' , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _text , 'my text' )
    def test_add_table_label_text_none ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _text , '' )
    def test_add_table_label_anchor ( self ) :
        self . _table_gui . add_table_label ( None , 'my anchor' , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _anchor , 'my anchor' )
    def test_add_table_label_borderwidth ( self ) :
        self . _table_gui . add_table_label ( None , None , 'my borderwidth' )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _borderwidth , 'my borderwidth' )
    def test_add_table_label_borderwidth_none ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _borderwidth , 1 )
    def test_add_table_label_relief ( self ) :
        self . _mediator . _relief_sunken = 'my sunken'
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _relief , 'my sunken' )
    def test_add_table_label_grid_widget ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_true ( label . _in_grid )
    def test_add_table_label_grid_sticky ( self ) :
        self . _mediator . _sticky_nesw = 'my sticky'
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _sticky , 'my sticky' )
    #
    # mouse_click_row_event
    #
    def test_create_mouse_click_event ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        event = self . _mediator . _events [ -1 ]
        self . _test . assume_equal ( event . _widget , label )
    def test_persist_mouse_click_event ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _mediator . _events = [ ]
        self . _test . assume_equal ( event_fake . _events_deleted , 0 )
    def test_delete_mouse_click_event ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _mediator . _events = [ ]
        self . _table_gui . create_new_table_in_frame ( self . _table_frame )
        self . _test . assume_equal ( event_fake . _events_deleted , 1 )
    def test_table_row_mouse_click ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _mediator . _events [ -1 ] . _handler ( )
        self . _test . assume_equal ( self . _mediator . _table_rows_clicked , [ 0 ] )
    def test_table_row_mouse_click_second_row ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        self . _table_gui . add_table_label ( None , None , None )
        self . _mediator . _events [ -1 ] . _handler ( )
        self . _test . assume_equal ( self . _mediator . _table_rows_clicked , [ 1 ] )
    #
    # _grid_widget
    #
    def test_grid_first_column ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _column , 0 )
    def test_grid_first_row ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _row , 0 )
    def test_grid_second_column ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _column , 1 )
    #
    # finish_table_row
    #
    def test_finish_table_row_reset_column ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _column , 0 )
    def test_finish_table_row_increment_row ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _row , 1 )
    #
    # select_table_row
    #
    def test_select_table_row_no_selection ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_empty ( label . _background_history )
    def test_select_table_row_single_label ( self ) :
        self . _mediator . _light_blue = 'my light blue'
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . select_table_row ( 0 )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_equal ( label . _background_history ,
            [ [ mediator_fake . gui_push_background_color ]
            , [ mediator_fake . gui_set_background_color , 'my light blue' ]
            ] )
    def test_select_table_row_whole_row ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . select_table_row ( 0 )
        for label in self . _mediator . _labels :
            self . _test . assume_not_empty ( label . _background_history )
    def test_select_table_row_after_finish_table_row ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . select_table_row ( 1 )
        label = self . _mediator . _labels [ -1 ]
        self . _test . assume_not_empty ( label . _background_history )
    def test_select_table_row_after_finish_table_row_unselected_row ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . select_table_row ( 1 )
        label = self . _mediator . _labels [ 0 ]
        self . _test . assume_empty ( label . _background_history )
    #
    # unselect_table_row
    #
    def test_unselect_table_row_single_label ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        label = self . _mediator . _labels [ 0 ]
        self . _table_gui . unselect_table_row ( 0 )
        self . _test . assume_equal ( label . _background_history , 
            [ [ mediator_fake . gui_pop_background_color ] 
            ] )
    def test_unselect_table_row_multiple_labels ( self ) :
        for i in xrange ( 3 ) :
            self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . unselect_table_row ( 0 )
        for label in self . _mediator . _labels :
            self . _test . assume_equal ( label . _background_history , 
                [ [ mediator_fake . gui_pop_background_color ] 
                ] )
    def test_unselect_table_row_after_finish_table_row ( self ) :
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . unselect_table_row ( 1 )
        label = self . _mediator . _labels [ 0 ]
        self . _test . assume_empty ( label . _background_history ) 
    #
    # add_table_entry
    #
    def test_add_table_entry_master_frame ( self ) :
        self . _table_gui . add_table_entry_request ( )
        entry = self . _mediator . _add_table_entry_reply
        self . _test . assume_equal ( entry . _master , self . _table_frame )
    def test_add_table_entry_in_grid ( self ) :
        self . _table_gui . add_table_entry_request ( )
        entry = self . _mediator . _add_table_entry_reply
        self . _test . assume_true ( entry . _in_grid )
    #
    # bold_table_row
    #
    def test_bold_table_row ( self ) :
        for i in xrange ( 3 ) :
            self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        for i in xrange ( 3 ) :
            self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . bold_table_row ( 1 )
        for label in self . _mediator . _labels [ : 3 ] :
            self . _test . assume_empty ( label . _font_history )
        for label in self . _mediator . _labels [ 3 : ] :
            self . _test . assume_equal ( label . _font_history , 
                [ mediator_fake . gui_push_widget_font
                , mediator_fake . gui_set_widget_font_weight_to_bold
                ] )
    #
    # unbold_table_row
    #
    def test_unbold_table_row ( self ) :
        for i in xrange ( 3 ) :
            self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . finish_table_row ( )
        for i in xrange ( 3 ) :
            self . _table_gui . add_table_label ( None , None , None )
        self . _table_gui . unbold_table_row ( 1 )
        for label in self . _mediator . _labels [ : 3 ] :
            self . _test . assume_empty ( label . _font_history )
        for label in self . _mediator . _labels [ 3 : ] :
            self . _test . assume_equal ( label . _font_history , 
                [ mediator_fake . gui_pop_widget_font
                ] )
